﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Globalization;

namespace consoleTesting
{
    class Program
    {

        //private static DateTime dtStartTest = new DateTime(2001, 02, 01);

        static void Main(string[] args)
        {
            BaseInit();
        }

        public static void BaseInit()
        {

            //List<Candle> weekCandles = BuildCandlesFromCsv("EURUSD10080.csv");
            //List<Candle> dayCandles = BuildCandlesFromCsv("EURUSD_day.csv");
            //List<CandleForexite> hourlyForexiteCandles = new List<CandleForexite>();//BuildForexiteCandlesFromCsv();

            //WeeklyGapsTest gapTest = new WeeklyGapsTest(weekCandles, dayCandles, hourlyForexiteCandles, 10000);
            List<CandleWSRT> m1Candles = BuildCandlesFromCsvFibo("candlesM1.csv");
            List<CandleWSRT> m15candles = BuildCandlesFromCsvFibo("candlesM15.csv");
            //GapsTestFibo fiboTest = new GapsTestFibo(minCandles);
            //DayTestUpDown dayUpDwonTest = new DayTestUpDown(minCandles);
            NetWsrt3_8 wsrt = new NetWsrt3_8(m1Candles, m15candles);

        }

        public static List<Candle> BuildCandlesFromCsv(string filePath)
        {
            string line = "";
            List<Candle> candles = new List<Candle>();


            if (File.Exists(filePath))
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

                StreamReader file = new StreamReader(filePath);
                string[] splitter = new string[] { "," };

                string[] objs;
                Candle candle = new Candle();

                int e = 0;
                while ((line = file.ReadLine()) != null)
                {
                    objs = line.Split(splitter, StringSplitOptions.RemoveEmptyEntries);
                    candle = new Candle();

                    candle.Time = Convert.ToDateTime(String.Format("{0} {1}", objs[0], objs[1]));
                    candle.Open = Math.Round(Convert.ToDecimal(objs[2]), 5);
                    candle.High = Math.Round(Convert.ToDecimal(objs[3]), 5);
                    candle.Low = Math.Round(Convert.ToDecimal(objs[4]), 5);
                    candle.Close = Math.Round(Convert.ToDecimal(objs[5]), 5);

                    if (candle.Open > candle.Close)
                        candle.Direction = Direction.DOWN;
                    if (candle.Open < candle.Close)
                        candle.Direction = Direction.UP;
                    if (candle.Open == candle.Close)
                        candle.Direction = Direction.DODGE;

                    candle.BodyWidth = Math.Round((Math.Abs(candle.Open - candle.Close) * 10000), 2);
                    //if(e<1000)
                    candles.Add(candle);
                    //Console.WriteLine(line);
                    e++;
                }

                String сandlesPath = String.Format("candles_{0}.xml", filePath.Replace(".csv", ""));
                Stream stream = File.Open(сandlesPath, FileMode.Create);

                System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(candles.GetType());
                x.Serialize(stream, candles);
                stream.Close();


            }

            return candles;
        }

        public static List<CandleWSRT> BuildCandlesFromCsvFibo(string filePath)
        {
            string line = "";
            List<CandleWSRT> candles = new List<CandleWSRT>();
            int candleIndex=0;

            //NetWSRTHelper helper = new NetWSRTHelper();

            if (File.Exists(filePath))
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

                StreamReader file = new StreamReader(filePath);
                string[] splitter = new string[] { ";" };

                string[] objs;
                CandleWSRT candle = new CandleWSRT();

                while ((line = file.ReadLine()) != null)
                {
                    if (candleIndex == 0)
                    {
                        candleIndex++;
                        continue;
                    }

                    objs = line.Split(splitter, StringSplitOptions.RemoveEmptyEntries);
                    candle = new CandleWSRT();
                    candle.CandleIndex = candleIndex;

                    candle.Time = Convert.ToDateTime(String.Format("{0}", objs[0]));
                    candle.Open = Math.Round(Convert.ToDecimal(objs[1]), 5);
                    candle.High = Math.Round(Convert.ToDecimal(objs[2]), 5);
                    candle.Low = Math.Round(Convert.ToDecimal(objs[3]), 5);
                    candle.Close = Math.Round(Convert.ToDecimal(objs[4]), 5);
                    if (objs.Count() > 5)
                    {
                        candle.iMA_Signal = Math.Round(Convert.ToDouble(objs[5]), 4);
                        candle.iCCI_Signal = Math.Round(Convert.ToDouble(objs[6]), 4);
                        candle.iATR_Signal = Math.Round(Convert.ToDouble(objs[7]), 4);
                        candle.iWPR_Signal = Math.Round(Convert.ToDouble(objs[8]), 4);
                    }

                    if (candle.Open > candle.Close)
                        candle.Direction = Direction.DOWN;
                    if (candle.Open < candle.Close)
                        candle.Direction = Direction.UP;
                    if (candle.Open == candle.Close)
                        candle.Direction = Direction.DODGE;

                    candle.BodyWidth = Math.Round((Math.Abs(candle.Open - candle.Close) * 10000), 2);
                    
                    candles.Add(candle);

                    candleIndex++;

                }

                String сandlesPath = String.Format("WSRT3_8/candles_{0}.xml", filePath.Replace(".csv", ""));
                Stream stream = File.Open(сandlesPath, FileMode.Create);

                System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(candles.GetType());
                x.Serialize(stream, candles);
                stream.Close();


            }

            return candles;

        }

        public static List<CandleForexite> BuildForexiteCandlesFromCsv()
        {
            string line = "";
            string resultFile = "candlesForexiteHourly.xml";
            string directory = "Forexite";

            if (File.Exists(resultFile))
                File.Delete(resultFile);

            List<CandleForexite> candles = new List<CandleForexite>();

            foreach (string filePath in Directory.GetFiles(directory))
            {
                if (File.Exists(filePath))
                {
                    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

                    StreamReader file = new StreamReader(filePath);
                    string[] splitter = new string[] { "," };

                    string[] objs;
                    CandleForexite candle = new CandleForexite();
                

                    int e = 0;
                    while ((line = file.ReadLine()) != null)
                    {
                        if (e == 0)
                        {
                            e++;
                            continue;
                        }

                        objs = line.Split(splitter, StringSplitOptions.RemoveEmptyEntries);

                        if (objs[0] != "EURUSD")
                            continue;

                        candle = new CandleForexite();

                        candle.Symbol = objs[0];
                        candle.Time = Convert.ToDateTime(String.Format("{0} {1}", GetValidDateForForexiteCsvObj(objs[1]), GetValidTimeForForexiteCsvObj(objs[2])));
                        candle.Open = Math.Round(Convert.ToDecimal(objs[3]), 5);
                        candle.High = Math.Round(Convert.ToDecimal(objs[4]), 5);
                        candle.Low = Math.Round(Convert.ToDecimal(objs[5]), 5);
                        candle.Close = Math.Round(Convert.ToDecimal(objs[6]), 5);

                        if (candle.Open > candle.Close)
                            candle.Direction = Direction.DOWN;
                        if (candle.Open < candle.Close)
                            candle.Direction = Direction.UP;
                        if (candle.Open == candle.Close)
                            candle.Direction = Direction.DODGE;

                        candle.BodyWidth = Math.Round((Math.Abs(candle.Open - candle.Close) * 10000), 2);
                        //if(e<1000)
                        candles.Add(candle);
                        //Console.WriteLine(line);
                        e++;
                    }
                }
            }

            String сandlesPath = String.Format("candlesForexiteHourly.xml");
            Stream stream = File.Open(сandlesPath, FileMode.Create);

            System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(candles.GetType());
            ser.Serialize(stream, candles);
            stream.Close();

            return candles.FindAll(x=>x.Symbol=="EURUSD").OrderBy(y => y.Time).ToList<CandleForexite>();
        }

        private static string GetValidDateForForexiteCsvObj(string obj)
        {
            string toReturn = obj;

            toReturn = obj.Insert(4, ".").Insert(7, ".");

            return toReturn;
        }

        private static string GetValidTimeForForexiteCsvObj(string obj)
        {
            string toReturn = obj;

            toReturn = obj.Insert(2, ":").Insert(5, ":");

            return toReturn;
        }
    }
}
